<template>   

    <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="120">
        <FormItem label="类型">
            <RadioGroup v-model="formItem.classification">
                <Radio label="1">加分</Radio>
                <Radio label="2">减分</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="状态">
            <RadioGroup v-model="formItem.type">
                <Radio label="1">迟到</Radio>
                <Radio label="2">旷工</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="分数">
            <InputNumber :min="0" v-model="formItem.fixed_score"></InputNumber>
        </FormItem>
        <FormItem label="备注" prop="remarks.zh">
            <Input v-model="formItem.remarks.zh" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="输入备注"></Input>            
        </FormItem>
        <FormItem>
            <Button type="primary" @click="submit('formItem')">保存</Button>
        </FormItem>
        <div style="clear:both"></div>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>
export default {
    props: {
        infoId: {
            type: [Boolean,Number],
            default: false
        }
    },
    
    data () {
        return {
            

            infoUrl:`api/watchlist/v1/admininspecttermrules/find/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式

            spinShow:false,
            infoOld:[],//请求的数据

            formItem: {      
                remarks: {
                    zh:null,
                    en:null,
                    mn:null,
                },
                type:'2',
                classification:'2',
                fixed_score:0,
            },
            ruleValidate: {}
            
          
        }
    },
    methods: {
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        submit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formItem).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    }) 
                } else {
                    this.$Message.error('验证失败');
                }
            })
        },
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用
            this.$http.get(this.infoUrl).then(val=>{ 
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formItem.remarks = this.infoOld.remarks
                this.formItem.classification = this.infoOld.classification.toString()
                this.formItem.type = this.infoOld.type.toString()
                this.formItem.fixed_score = parseInt(this.infoOld.fixed_score)
                this.spinShow=false//输入框禁用 
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
            })
        },
        
        
        /************************************************************
         * 
         * 开始时间
         * 
         ************************************************************/
        time_anfun(obj){
            this.formItem.time_an=obj

        },
        
        /************************************************************
         * 
         * 结束时间
         * 
         ************************************************************/
        time_enfun(obj){
            this.formItem.time_en=obj

        },
        
        /************************************************************
         * 
         * 点击学年学期请求列表数据
         * 
         ************************************************************/
        peoplebutton(val){
            this.formItem.schoolyear_id=val[0]
            this.formItem.semester_id=val[1]
        },
        
    },
    mounted(){
        if(this.infoId==false){
            // add
            this.xspd=true
            this.UpImgShow=true
            this.saveUrl='api/watchlist/v1/admininspecttermrules/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/watchlist/v1/admininspecttermrules/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
}
</script>
